package com.zyht.bank.wechat.http;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wx.model.BussinessException;
import com.wx.util.JsonUtil;

public interface HttpCommon {
	static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	static SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

	/**
	 * 输出日志
	 * 
	 * @param msg
	 */

	default <T extends ResObject> T exeAjax(Class<T> clazz, AjaxMain<T> ajaxMain) {
		T res = null;
		String msg = "";
		try {
			res = clazz.newInstance();
			ajaxMain.exe(res);// TODO 做一些业务
			if (res.getCode() == null || res.getCode().isEmpty()) {
				res.setCode(EnumCode.error);
			}
			return res;
		} catch (Exception e) {
			if (e instanceof BussinessException) {
				msg = e.getMessage();
			} else {
				msg = "system error";
				e.printStackTrace();
			}
		}
		if (res == null) {
			return null;
		} else {
			res.setMsg(msg);
			if (res.getCode() == null || res.getCode().isEmpty()) {
				res.setCode(EnumCode.error);
			}
		}
		return res;
	}

	default <T extends ResObject> String cvJson(T t) {
		return JsonUtil.toJSONString(t);
	}

	default void writeJson(HttpServletResponse response, String json) {
		response.setContentType("application/json;charset=UTF-8");
		write(response, json);
	}

	default void writePlain(HttpServletResponse response, String json) {
		response.setContentType("text/plain;charset=UTF-8");
		write(response, json);
	}

	default void writeHtml(HttpServletResponse response, String str) {
		response.setContentType("text/html;charset=UTF-8");
		write(response, str);
	}

	default void write(HttpServletResponse response, String str) {
		PrintWriter pw;
		try {
			pw = response.getWriter();
			pw.write(str);
			pw.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	default String pString(HttpServletRequest request, String str) {
		try {
			String s = request.getParameter(str);
			if (s == null || s.isEmpty()) {
				return null;
			}
			return s;
		} catch (Exception e) {
			return null;
		}
	}

	default Integer pInteger(HttpServletRequest request, String str) {
		try {
			return Integer.parseInt(request.getParameter(str));
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * yyyy-MM-dd HH:mm:ss
	 * 
	 * @param request
	 * @param str
	 * @return
	 */
	default Date pDate(HttpServletRequest request, String str) {
		try {
			return dateFormat.parse(request.getParameter(str));
		} catch (Exception e) {
			return null;
		}
	}

	default Long pLong(HttpServletRequest request, String str) {
		try {
			return Long.parseLong(request.getParameter(str));
		} catch (Exception e) {
			return null;
		}
	}

	default BigDecimal pBigDecimal(HttpServletRequest request, String str) {
		try {
			BigDecimal bigDecimal = new BigDecimal(request.getParameter(str));
			return new BigDecimal(bigDecimal.toPlainString());
		} catch (Exception e) {
			return null;
		}
	}

	default Cookie pCookie(HttpServletRequest request, String str) {
		if (str == null) {
			return null;
		}
		Cookie[] c = request.getCookies();
		if(c==null || c.length==0){
			return null;
		}
		for (Cookie cookie : c) {
			if (str.equals(cookie.getName())) {
				return cookie;
			}
		}
		return null;
	}
	
	/**
	 * 插入
	 * @param request
	 * @param str
	 * @param obj
	 */
	default void iSession(HttpServletRequest request, String str, Object obj) {
		if (request==null || str == null) {
			return;
		}
		request.getSession().setAttribute(str, obj);
		return;
	}
	
	/**
	 * 删除
	 * @param request
	 * @param str
	 */
	default void dSession(HttpServletRequest request, String str) {
		if (request==null || str == null) {
			return;
		}
		request.getSession().removeAttribute(str);
		return;
	}
	
	@SuppressWarnings("unchecked")
	default <T> T sSession(HttpServletRequest request, String str,Class<T> clazz) {
		if (request==null || str == null) {
			return null;
		}
		
		Object object = request.getSession().getAttribute(str);
		
		if(object == null){
			return null;
		}
		if(object.getClass().isAssignableFrom(clazz)){
				return (T) object;
		}
		return null;
	}

	/**
	 * 获取项目文件系统根路径
	 * 
	 * @param request
	 * @return
	 */
	default String getRealPath(HttpServletRequest request) {
		return request.getServletContext().getRealPath("/");
	}
	
	default String getUrlPath(HttpServletRequest request) {
		String refererUrl = request.getScheme().concat("://").concat(request.getServerName());
		int port = request.getServerPort();
		if (port > 0 && port != 80) {
			refererUrl = refererUrl.concat(":").concat(port + "");
		}
		return refererUrl.concat(request.getContextPath());
	}

	default String getRefererUrl(HttpServletRequest request) {
		String refererUrl = getUrlPath(request).concat(request.getServletPath());
		String qs = request.getQueryString();
		if (qs != null && !qs.isEmpty()) {
			refererUrl = refererUrl.concat("?").concat(qs);
		}
		return refererUrl;
	}

}
